package com.duobaoyu.dby.supply.admin.controller.config;

import cn.hutool.script.ScriptUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.util.StringUtil;
import com.duobaoyu.dby.common.core.validator.groups.Add;
import com.duobaoyu.dby.common.core.validator.groups.Edit;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceChannelRuleConfigAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.ChannelRuleConfigBizDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.ChannelRuleConfigQueryBizDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.ScriptDebugBizDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.ChannelRuleConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.script.SimpleBindings;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道规则配置
 * @author liyang
 * @version 1.0.0
 * @ClassName ChannelRuleConfigController
 * @Description 渠道规则配置
 * @createTime 13:49:00 2023/05/08
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/rule-config")
@Api(value = "channel-rule-config", tags = "渠道规则配置表")
public class ChannelRuleConfigController {

    private final ResourceChannelRuleConfigAdminClient resourceChannelRuleConfigAdminClient;

    /**
     * 添加渠道规则配置
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "添加渠道规则配置", notes = "添加渠道规则配置")
    @SupplySysLog(value = "添加渠道规则配置",operationType = OperationTypeEnum.INTERT)
    public R<Boolean> createRuleConfig(@Validated(Add.class) @RequestBody ChannelRuleConfigBizDTO channelRuleConfig) {

        return R.ok(resourceChannelRuleConfigAdminClient.createRuleConfig(channelRuleConfig));
    }

    /**
     * 修改渠道规则配置
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改渠道规则配置", notes = "修改渠道规则配置")
    @SupplySysLog(value = "修改渠道规则配置",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> updateRuleConfig(@Validated(Edit.class) @RequestBody ChannelRuleConfigBizDTO channelRuleConfig) {

        return R.ok(resourceChannelRuleConfigAdminClient.updateRuleConfig(channelRuleConfig));
    }

    /**
     * 删除渠道规则配置
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除渠道规则配置", notes = "删除渠道规则配置")
    @SupplySysLog(value = "删除渠道规则配置",operationType = OperationTypeEnum.DELETE)
    public R<Boolean> deleteRuleConfig(@NotEmpty(message = "配置主键ID不能为空") @RequestBody List<Long> channelRuleConfigIds) {

        return R.ok(resourceChannelRuleConfigAdminClient.deleteRuleConfig(channelRuleConfigIds));
    }

    /**
     * 分页查询渠道规则配置
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询渠道规则配置", notes = "分页查询渠道规则配置")
    @SupplySysLog(value = "分页查询渠道规则配置",operationType = OperationTypeEnum.SELECT)
    public R<Page<ChannelRuleConfigVO>> page(ChannelRuleConfigQueryBizDTO ruleConfigQuery){

        return R.ok(resourceChannelRuleConfigAdminClient.channelRuleConfigPage(ruleConfigQuery));
    }

    /**
     * 渠道规则配置脚本调试
     * @return
     */
    @PostMapping("/script/debug")
    @ApiOperation(value = "渠道规则配置脚本调试", notes = "渠道规则配置脚本调试")
    @SupplySysLog(value = "渠道规则配置脚本调试",operationType = OperationTypeEnum.SELECT)
    public R<Object> scriptDebug(@Validated @RequestBody ScriptDebugBizDTO scriptDebugBiz) {
        HashMap<String, Object> hashMap = new HashMap<>();
        SimpleBindings bindings = new SimpleBindings();
        if (StringUtil.isNotBlank(scriptDebugBiz.getParams())) {
            bindings.putAll(JSONObject.parseObject(scriptDebugBiz.getParams(), new TypeReference<Map<String, Object>>() {
            }));
        }
        hashMap.put("后管结果", ScriptUtil.eval(scriptDebugBiz.getScript(), bindings));
        Object o = resourceChannelRuleConfigAdminClient.scriptDebug(scriptDebugBiz);
        hashMap.put("resource结果",o);
        return R.ok(hashMap);
    }
}
